﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using D2DEngine.ParticleEffect;
using D2DEngine.Util;
using D2DEngine.Core;

namespace AIDev.Components.Projectiles
{
    public class MissileComponent : ProjectileComponent
    {
        public override void OnRegister()
        {
            this.impulseOnCollision = true;
            this.speed = 0.2f;
            this.damageAmount = 50.0f;
            this.damageRadius = 100.0f;

            base.OnRegister();
        }

        public override void OnUnRegister()
        {
            /*
            D2DAudioManager.Instance.PlayCue("Explosion01");

            D2DParticleEffect debris = D2DSceneManager.Instance.CurrentScene.FindObject<D2DParticleEffect>("ExplosionDebris_effect");
            debris.Position = this.actor.Position;
            debris.Spawn();

            D2DParticleEffect smoke = D2DSceneManager.Instance.CurrentScene.FindObject<D2DParticleEffect>("SmokeExplosion_effect");
            smoke.Position = this.actor.Position;
            smoke.Spawn();
            */

            D2DParticleEffect fire = this.actor.Scene.FindObject<D2DParticleEffect>("Effect_explosionFire");
            fire.Position = D2DMath.ToPixels(this.actor.Position);
            fire.Spawn();

            this.actor.Scene.Camera.Shake();

            D2DAssetDatabase.Instance.GetSound("Explosion").Play();

            /*
            // Radius blast
            List<D2DSceneObject> collectedObjects = D2DSceneManager.Instance.CurrentScene.Collect(this.actor.Position, this.damageRadius);
            D2DActor victim;

            Vector2 impulseDirection = Vector2.Zero;
            float impulseAngle = 0.0f;

            foreach (D2DSceneObject sceneObject in collectedObjects)
            {
                if (sceneObject is D2DActor)
                {
                    victim = sceneObject as D2DActor;

                    if (victim.GamePlay != null)
                    {
                        victim.GamePlay.Hit(this.damageAmount / 2);
                        impulseAngle = (float)Math.Atan((double)(this.actor.Position.Y - sceneObject.Position.Y) / (this.actor.Position.X - sceneObject.Position.X));

                        if (sceneObject.Position.X < this.actor.Position.X)
                        {
                            impulseAngle -= MathHelper.Pi;
                        }

                        impulseDirection = new Vector2((float)Math.Cos(impulseAngle), (float)Math.Sin(impulseAngle));
                        victim.Body.ApplyImpulse(impulseDirection * 100);
                    }
                }
            }
            */

            base.OnUnRegister();
        }

        /// <summary>
        /// Updates the projectile
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            // Spawn a smoke trail
            //D2DParticleEffect smokeTrail = D2DSceneManager.Instance.CurrentScene.FindObject<D2DParticleEffect>("SmokeTrail_effect");
           // smokeTrail.Position = this.actor.Position;
            //smokeTrail.Spawn();

            base.Update(gameTime);
        }
    }
}
